home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / storage / ipc / sinval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.2 KB  |  171 lines

  1. /*
  2.  * sinval.c --
  3.  *  POSTGRES shared cache invalidation communication code.
  4.  */
  5.  
  6. /* #define INVALIDDEBUG    1 */
  7.  
  8. #include "tmp/postgres.h"
  9.  
  10. #include "storage/sinval.h"
  11. #include "storage/sinvaladt.h"
  12. #include "storage/spin.h"
  13. #include "utils/log.h"
  14.  
  15.  
  16. RcsId("$Header: /private/postgres/src/storage/ipc/RCS/sinval.c,v 1.11 1992/07/30 19:51:20 mer Exp $");
  17.  
  18. #ifdef TEST                 /* XXX REMOVE for INTEGRATION  */
  19. void
  20. PRT(data)
  21. int  data;
  22. { printf(">>Cache Id %3d written.\n", data);
  23. }
  24. #endif                        /* XXX REMOVE for INTEGRATION  */
  25.  
  26.  
  27.  
  28. static SharedInvalid    Invalid = NULL;
  29. extern SISeg        *shmInvalBuffer;/* the shared buffer segment, set by*/
  30.                                         /*   SISegmentAttach()                */
  31. extern BackendId    MyBackendId;
  32. extern BackendTag    MyBackendTag;
  33.  
  34. SPINLOCK        SInvalLock = (SPINLOCK) NULL;
  35.  
  36. /****************************************************************************/
  37. /*  CreateSharedInvalidationState(key)   Create a buffer segment            */
  38. /*                                                                          */
  39. /*  should be called only by the POSTMASTER                                 */
  40. /****************************************************************************/
  41. void
  42. CreateSharedInvalidationState(key)
  43.     IPCKey    key;
  44. {
  45.     int    status;
  46.  
  47.     /* REMOVED
  48.     SISyncKill(IPCKeyGetSIBufferMemorySemaphoreKey(key));
  49.     SISyncInit(IPCKeyGetSIBufferMemorySemaphoreKey(key));
  50.     */
  51.  
  52.     /* SInvalLock gets set in spin.c, during spinlock init */
  53.     status = SISegmentInit(true, IPCKeyGetSIBufferMemoryBlock(key));
  54.     
  55.     if (status == -1) {
  56.         elog(FATAL, "CreateSharedInvalidationState: failed segment init");
  57.     }
  58. }
  59. /****************************************************************************/
  60. /*  AttachSharedInvalidationState(key)   Attach a buffer segment            */
  61. /*                                                                          */
  62. /*  should be called only by the POSTMASTER                                 */
  63. /****************************************************************************/
  64. void
  65. AttachSharedInvalidationState(key)
  66.     IPCKey    key;
  67. {
  68.     int    status;
  69.  
  70.     if (key == PrivateIPCKey) {
  71.           CreateSharedInvalidationState(key);
  72.           return;
  73.         }
  74.     /* SInvalLock gets set in spin.c, during spinlock init */
  75.     status = SISegmentInit(false, IPCKeyGetSIBufferMemoryBlock(key));
  76.     
  77.     if (status == -1) {
  78.         elog(FATAL, "AttachSharedInvalidationState: failed segment init");
  79.     }
  80. }
  81.  
  82. void
  83. InitSharedInvalidationState()
  84. {
  85.     SpinAcquire(SInvalLock);
  86.     if (!SIBackendInit(shmInvalBuffer))
  87.     {
  88.         SpinRelease(SInvalLock);
  89.         elog(FATAL, "Backend cache invalidation initialization failed");
  90.     }
  91.     SpinRelease(SInvalLock);
  92. }
  93.  
  94. /****************************************************************************/
  95. /*  RegisterSharedInvalid(cacheId, hashIndex, pointer)                      */
  96. /*                                                                          */
  97. /*  register a message in the buffer                                        */
  98. /*  should be called by a backend                                           */
  99. /****************************************************************************/
  100. void
  101. RegisterSharedInvalid(cacheId, hashIndex, pointer)
  102.     int        cacheId;    /* XXX */
  103.     Index       hashIndex;
  104.     ItemPointer    pointer;
  105. {
  106.     SharedInvalidData   newInvalid;
  107.     int                    status;
  108.  
  109. /*
  110.  * This code has been hacked to accept two types of messages.  This might
  111.  * be treated more generally in the future.
  112.  *
  113.  * (1)
  114.  *    cacheId= system cache id
  115.  *    hashIndex= system cache hash index for a (possibly) cached tuple
  116.  *    pointer= pointer of (possibly) cached tuple
  117.  *
  118.  * (2)
  119.  *    cacheId= special non-syscache id
  120.  *    hashIndex= object id contained in (possibly) cached relation descriptor
  121.  *    pointer= null
  122.  */
  123.  
  124.     newInvalid.cacheId = cacheId;
  125.     newInvalid.hashIndex = hashIndex;
  126.  
  127.     if (ItemPointerIsValid(pointer)) {
  128.     ItemPointerCopy(pointer, &newInvalid.pointerData);
  129.     } else {
  130.     ItemPointerSetInvalid(&newInvalid.pointerData);
  131.     }
  132.    
  133.     SpinAcquire(SInvalLock);
  134.     if (!SISetDataEntry(shmInvalBuffer, &newInvalid)) {
  135.         /* buffer full */
  136.         /* release a message, mark process cache states to be invalid */
  137.         SISetProcStateInvalid(shmInvalBuffer);
  138.  
  139.         if (!SIDelDataEntry(shmInvalBuffer)) {
  140.             /* inconsistent buffer state -- shd never happen */
  141.         SpinRelease(SInvalLock);
  142.             elog(FATAL, "RegisterSharedInvalid: inconsistent buffer state");
  143.         }
  144.  
  145.         /* write again */
  146.         (void) SISetDataEntry(shmInvalBuffer, &newInvalid);
  147.     }
  148.     SpinRelease(SInvalLock);
  149. }
  150. /****************************************************************************/
  151. /*  InvalidateSharedInvalid(invalFunction, resetFunction)                    */
  152. /*                                                                          */
  153. /*  invalidate a message in the buffer     (read and clean up)                */
  154. /*  should be called by a backend                                           */
  155. /****************************************************************************/
  156. void
  157. InvalidateSharedInvalid(invalFunction, resetFunction)
  158.     void        (*invalFunction)();
  159.     void        (*resetFunction)();
  160. {
  161.     SharedInvalid   temporaryInvalid;
  162.     int                status;
  163.     
  164.     SpinAcquire(SInvalLock);
  165.     SIReadEntryData(shmInvalBuffer, MyBackendId, 
  166.                     invalFunction, resetFunction);  
  167.                      
  168.     SIDelExpiredDataEntries(shmInvalBuffer);
  169.     SpinRelease(SInvalLock);
  170. }
  171.